/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.QueryHint;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import com.eelpo.cmmp.core.hibernate.orm.QueryHints;
import com.eelpo.common.util.BlankUtils;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * <pre>
 * 业务实体类型类，用于映射T_ENTITY_TYPE表(Persist Object)
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Entity
@Cacheable
@Table(name = "T_ENTITY_TYPE")
@NamedQuery(name = "EntityType.findByCode", query = "FROM TEntityType WHERE code = ?", hints = { @QueryHint(name = QueryHints.HINT_CACHEABLE, value = "true") })
public class TEntityType implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -1471439674817223793L;

	/**
	 * 实体类型Id
	 */
	@NotEmpty
	private String id;

	/**
	 * 实体类型名称
	 */
	@NotEmpty
	private String name;

	/**
	 * 实体类型代码
	 */
	@NotEmpty
	private String code;

	/**
	 * 实体类型状态
	 */
	@NotNull
	private Byte status;

	/**
	 * 使用该实体类型的所有权限
	 */
	private Set<TEntity> entities = new HashSet<TEntity>();

	public TEntityType() {

	}

	public TEntityType(String id) {
		this.id = id;
	}

	@Id
	@Column(length = 36)
	@GenericGenerator(name = "uuid", strategy = "uuid2")
	@GeneratedValue(generator = "uuid")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(nullable = false, length = 64)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(nullable = false, length = 64, unique = true)
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Column(nullable = false)
	public Byte getStatus() {
		return status;
	}

	public void setStatus(Byte status) {
		this.status = status;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "type")
	public Set<TEntity> getEntities() {
		return entities;
	}

	public void setEntities(Set<TEntity> entities) {
		this.entities = entities;
	}

	@Override
	public boolean equals(Object obj) {
		if (BlankUtils.isBlank(obj) || !(obj instanceof TEntityType))
			return false;

		if (this == obj)
			return true;

		TEntityType type = (TEntityType) obj;

		if (BlankUtils.isBlank(this.id) || BlankUtils.isBlank(type.getId())) {
			return false;
		}

		return this.id.equals(type.getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return "TEntityType[name=" + name + "]";
	}
}
